package com.heima.behavior.service.impl;

import com.heima.behavior.service.ApArticleBehaviorService;
import com.heima.behavior.service.ApBehaviorEntryService;
import com.heima.common.user.UserRelationConstants;
import com.heima.model.behavior.dtos.ArticleBehaviorDTO;
import com.heima.model.behavior.pojos.ApBehaviorEntry;
import com.heima.model.behavior.pojos.ApCollection;
import com.heima.model.behavior.pojos.ApLikesBehavior;
import com.heima.model.behavior.pojos.ApUnlikesBehavior;
import com.heima.model.behavior.vos.ArticleBehaviorVo;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.threadlocal.AppThreadLocalUtils;
import com.heima.model.user.pojos.ApUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;


@Service
public class ApArticleBehaviorServiceImpl implements ApArticleBehaviorService {

    @Autowired
    MongoTemplate mongoTemplate;
    @Autowired
    ApBehaviorEntryService apBehaviorEntryService;
    @Autowired
    StringRedisTemplate redisTemplate;

    @Override
    public ResponseResult loadArticleBehavior(ArticleBehaviorDTO dto) {
        ArticleBehaviorVo articleBehaviorVo = new ArticleBehaviorVo();
        articleBehaviorVo.setIslike(false);
        articleBehaviorVo.setIsunlike(false);
        articleBehaviorVo.setIscollection(false);
        articleBehaviorVo.setIsfollow(false);

        //1. 判断用户是否登录
        ApUser user = AppThreadLocalUtils.getUser();
        //2. 未登录 直接将 4种行为 均返回false
        if (user == null){
            return ResponseResult.okResult(articleBehaviorVo);
        }
            //3. 如果登录
            //4. 查询行为实体
            Integer loginId = user.getId();
            Integer equipmentId = dto.getEquipmentId();
            ApBehaviorEntry apBehaviorEntry = apBehaviorEntryService.findByUserIdOrEquipmentId(loginId, equipmentId);
            String entryId = apBehaviorEntry.getId();
            Long articleId = dto.getArticleId();
            //5. 根据行为实体 文章ID查询 是否点赞
            Query queryLikesBehavior = Query.query(Criteria.where("entryId").is(entryId).and("articleId").is(articleId));
            ApLikesBehavior apLikesBehavior = mongoTemplate.findOne(queryLikesBehavior, ApLikesBehavior.class);
            if (apLikesBehavior != null){
                articleBehaviorVo.setIslike(true);
            }
            //6. 根据行为实体 文章ID查询 是否不喜欢
            Query queryUnlikesBehavior = Query.query(Criteria.where("entryId").is(entryId).and("articleId").is(articleId));
            ApUnlikesBehavior apUnlikesBehavior = mongoTemplate.findOne(queryUnlikesBehavior, ApUnlikesBehavior.class);
            if (apUnlikesBehavior != null){
                articleBehaviorVo.setIsunlike(true);
            }
            //7. 根据行为实体 文章ID查询 是否收藏
            Query queryCollection = Query.query(Criteria.where("entryId").is(entryId).and("articleId").is(articleId));
            ApCollection apCollection = mongoTemplate.findOne(queryCollection, ApCollection.class);
            if (apCollection != null){
                articleBehaviorVo.setIscollection(true);
            }
            //8. 根据登录用户id 去redis中查询是否关注该作者
            int  loginUserId = user.getId();
            int authorUserId = dto.getAuthorApUserId();
            Double score = redisTemplate.opsForZSet().score(UserRelationConstants.FOLLOW_LIST + loginUserId, String.valueOf(authorUserId));
            if (score != null){
                articleBehaviorVo.setIsfollow(true);
            }
            //9. 封装结果 返回
            return ResponseResult.okResult(articleBehaviorVo);

    }
}
